// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

package com.android.tools.r8.desugar.bridge;

import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

public class LambdaWithMultipleImplementingInterfacesCovariantDump implements Opcodes {

  // Generated from LambdaWithMultipleImplementingInterfaces with the bootstrapmethod changed from:
  //   0: #47 invokestatic java/lang/invoke/LambdaMetafactory.altMetafactory:...
  //     Method arguments:
  //       #48 ()Ljava/lang/String;
  //       #49 invokestatic com/android/tools/r8/desugar/bridge/
  //          LambdaWithMultipleImplementingInterfaces.lambda$main$0:()Ljava/lang/String;
  //       #48 ()Ljava/lang/String;
  //       #50 4
  //       #51 1
  //       #52 ()Ljava/lang/Object;
  // to
  //   0: #47 invokestatic java/lang/invoke/LambdaMetafactory.altMetafactory:...
  //     Method arguments:
  //       #48 ()Ljava/lang/Object;
  //       #49 invokestatic com/android/tools/r8/desugar/bridge/
  //          LambdaWithMultipleImplementingInterfaces.lambda$main$0:()Ljava/lang/String;
  //       #48 ()Ljava/lang/String;
  //       #50 4
  //       #51 1
  //       #52 ()Ljava/lang/String;
  // This will need to create a bridge method that takes in an Object and returns a String, which
  // is seen in class-files generated by the Scala-compiler.
  public static byte[] dump() {

    ClassWriter classWriter = new ClassWriter(0);
    MethodVisitor methodVisitor;

    classWriter.visit(
        V1_8,
        ACC_PUBLIC | ACC_SUPER,
        "com/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces",
        null,
        "java/lang/Object",
        null);

    classWriter.visitSource("LambdaWithMultipleImplementingInterfaces.java", null);

    classWriter.visitInnerClass(
        "com/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces$K",
        "com/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces",
        "K",
        ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE);

    classWriter.visitInnerClass(
        "com/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces$J",
        "com/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces",
        "J",
        ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE);

    classWriter.visitInnerClass(
        "com/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces$I",
        "com/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces",
        "I",
        ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE);

    classWriter.visitInnerClass(
        "java/lang/invoke/MethodHandles$Lookup",
        "java/lang/invoke/MethodHandles",
        "Lookup",
        ACC_PUBLIC | ACC_FINAL | ACC_STATIC);

    {
      methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
      methodVisitor.visitCode();
      Label label0 = new Label();
      methodVisitor.visitLabel(label0);
      methodVisitor.visitLineNumber(7, label0);
      methodVisitor.visitVarInsn(ALOAD, 0);
      methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
      methodVisitor.visitInsn(RETURN);
      Label label1 = new Label();
      methodVisitor.visitLabel(label1);
      methodVisitor.visitLocalVariable(
          "this",
          "Lcom/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces;",
          null,
          label0,
          label1,
          0);
      methodVisitor.visitMaxs(1, 1);
      methodVisitor.visitEnd();
    }
    {
      methodVisitor =
          classWriter.visitMethod(
              ACC_PUBLIC | ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
      methodVisitor.visitCode();
      Label label0 = new Label();
      methodVisitor.visitLabel(label0);
      methodVisitor.visitLineNumber(22, label0);
      methodVisitor.visitInvokeDynamicInsn(
          "get",
          "()Lcom/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces$K;",
          new Handle(
              Opcodes.H_INVOKESTATIC,
              "java/lang/invoke/LambdaMetafactory",
              "altMetafactory",
              "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;",
              false),
          new Object[] {
            Type.getType("()Ljava/lang/Object;"),
            new Handle(
                Opcodes.H_INVOKESTATIC,
                "com/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces",
                "lambda$main$0",
                "()Ljava/lang/String;",
                false),
            Type.getType("()Ljava/lang/String;"),
            new Integer(4),
            new Integer(1),
            Type.getType("()Ljava/lang/String;")
          });
      methodVisitor.visitVarInsn(ASTORE, 1);
      Label label1 = new Label();
      methodVisitor.visitLabel(label1);
      methodVisitor.visitLineNumber(23, label1);
      methodVisitor.visitVarInsn(ALOAD, 1);
      methodVisitor.visitMethodInsn(
          INVOKESTATIC,
          "com/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces",
          "testI",
          "(Lcom/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces$I;)V",
          false);
      Label label2 = new Label();
      methodVisitor.visitLabel(label2);
      methodVisitor.visitLineNumber(24, label2);
      methodVisitor.visitVarInsn(ALOAD, 1);
      methodVisitor.visitMethodInsn(
          INVOKESTATIC,
          "com/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces",
          "testJ",
          "(Lcom/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces$J;)V",
          false);
      Label label3 = new Label();
      methodVisitor.visitLabel(label3);
      methodVisitor.visitLineNumber(25, label3);
      methodVisitor.visitInsn(RETURN);
      Label label4 = new Label();
      methodVisitor.visitLabel(label4);
      methodVisitor.visitLocalVariable("args", "[Ljava/lang/String;", null, label0, label4, 0);
      methodVisitor.visitLocalVariable(
          "k",
          "Lcom/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces$K;",
          null,
          label1,
          label4,
          1);
      methodVisitor.visitMaxs(1, 2);
      methodVisitor.visitEnd();
    }
    {
      methodVisitor =
          classWriter.visitMethod(
              ACC_PRIVATE | ACC_STATIC,
              "testI",
              "(Lcom/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces$I;)V",
              null,
              null);
      methodVisitor.visitCode();
      Label label0 = new Label();
      methodVisitor.visitLabel(label0);
      methodVisitor.visitLineNumber(28, label0);
      methodVisitor.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
      methodVisitor.visitVarInsn(ALOAD, 0);
      methodVisitor.visitMethodInsn(
          INVOKEINTERFACE,
          "com/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces$I",
          "get",
          "()Ljava/lang/Object;",
          true);
      methodVisitor.visitMethodInsn(
          INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/Object;)V", false);
      Label label1 = new Label();
      methodVisitor.visitLabel(label1);
      methodVisitor.visitLineNumber(29, label1);
      methodVisitor.visitInsn(RETURN);
      Label label2 = new Label();
      methodVisitor.visitLabel(label2);
      methodVisitor.visitLocalVariable(
          "i",
          "Lcom/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces$I;",
          null,
          label0,
          label2,
          0);
      methodVisitor.visitMaxs(2, 1);
      methodVisitor.visitEnd();
    }
    {
      methodVisitor =
          classWriter.visitMethod(
              ACC_PRIVATE | ACC_STATIC,
              "testJ",
              "(Lcom/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces$J;)V",
              null,
              null);
      methodVisitor.visitCode();
      Label label0 = new Label();
      methodVisitor.visitLabel(label0);
      methodVisitor.visitLineNumber(32, label0);
      methodVisitor.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
      methodVisitor.visitVarInsn(ALOAD, 0);
      methodVisitor.visitMethodInsn(
          INVOKEINTERFACE,
          "com/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces$J",
          "get",
          "()Ljava/lang/String;",
          true);
      methodVisitor.visitMethodInsn(
          INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
      Label label1 = new Label();
      methodVisitor.visitLabel(label1);
      methodVisitor.visitLineNumber(33, label1);
      methodVisitor.visitInsn(RETURN);
      Label label2 = new Label();
      methodVisitor.visitLabel(label2);
      methodVisitor.visitLocalVariable(
          "j",
          "Lcom/android/tools/r8/desugar/bridge/LambdaWithMultipleImplementingInterfaces$J;",
          null,
          label0,
          label2,
          0);
      methodVisitor.visitMaxs(2, 1);
      methodVisitor.visitEnd();
    }
    {
      methodVisitor =
          classWriter.visitMethod(
              ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC,
              "lambda$main$0",
              "()Ljava/lang/String;",
              null,
              null);
      methodVisitor.visitCode();
      Label label0 = new Label();
      methodVisitor.visitLabel(label0);
      methodVisitor.visitLineNumber(22, label0);
      methodVisitor.visitLdcInsn("Hello World!");
      methodVisitor.visitInsn(ARETURN);
      methodVisitor.visitMaxs(1, 0);
      methodVisitor.visitEnd();
    }
    classWriter.visitEnd();

    return classWriter.toByteArray();
  }
}
